package com.atguigu.lease.web.admin;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.model.entity.UserInfo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import nonapi.io.github.classgraph.json.Id;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.crypto.SecretKey;
import java.util.Collections;
import java.util.Date;
import java.util.Random;
import java.util.stream.IntStream;

/**
 * Author ChenYongSheng
 * Date 2025/9/30 20:17
 */

@SpringBootTest
public class test {

    @Autowired
    MinioProperties minioProperties;

    @Test
    public void test01(){
        String endpoint = minioProperties.getEndpoint();
        System.out.println("endpoint = " + endpoint);
    }

    @Test
    public void test02(){
        Random random =new Random();
        StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i < 4; i++){
            stringBuilder.append(random.nextInt(10));
        }
        String string = stringBuilder.toString();
        System.out.println("string = " + string);
        System.out.println("random = " + random);
    }

    @Test
    public void setToken(){

        UserInfo userInfo =new UserInfo();
        userInfo.setId(1L);
        userInfo.setPhone("18605002028");

        String key ="999999999999ALKSHFOIASJFOIQUWR9999999999999999999ASOHFASF999999";

        SecretKey secretKey = Keys.hmacShaKeyFor(key.getBytes());

        //过期时间
        long tokenExpiration =60*60*1000L;


        String compact = Jwts.builder()
                .claim("userId", userInfo.getId())
                .claim("userPhone", userInfo.getPhone())
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration)) //设置过期时间
                .signWith(secretKey)
                .compact();

        System.out.println("compact = " + compact);
    }

    @Test
    public void getToken(){
        //用于解析的token
        String token ="eyJhbGciOiJIUzM4NCJ9.eyJ1c2VySWQiOjEsInVzZXJQaG9uZSI6IjE4NjA1MDAyMDI4IiwiZXhwIjoxNzYwNDM1NTUwfQ.u47lyJ-mzCKHZtFs9JnaXMg6UsXMkUDif-PA0di36m7GATALtpXHVcZYCbeJI-Hk";

        //秘钥
        String key ="999999999999ALKSHFOIASJFOIQUWR9999999999999999999ASOHFASF999999";

        // 获取到秘钥
        SecretKey secretKey = Keys.hmacShaKeyFor(key.getBytes());

        //parser反序列化
        JwtParser jwtParser = Jwts.parserBuilder().setSigningKey(secretKey).build();


        //对token进行反序列化
        Jws<Claims> claimsJws = jwtParser.parseClaimsJws(token);
        Claims claims = claimsJws.getBody();

        //提取token中的数据
        UserInfo  userInfo = new UserInfo();
        userInfo.setId(claims.get("userId",Long.class));
        userInfo.setPhone(claims.get("userPhone",String.class));

        Long id = userInfo.getId();
        System.out.println("id = " + id);

        String phone = userInfo.getPhone();
        System.out.println("phone = " + phone);

        System.out.println("claimsJws = " + claimsJws);

    }
}
